package com.solo.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.solo.model.Question;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet("/api/exam")
public class ExamServlet extends HttpServlet {
    private Gson gson = new Gson();
    private List<Question> questions;

    @Override
    public void init() {
        questions = new ArrayList<>();

        // 添加单选题
        for (int i = 1; i <= 5; i++) {
            Question q = new Question();
            q.setId(i);
            q.setType("single");
            q.setQuestion("单选题" + i + ": 这是一个示例单选题？");
            q.setOptions(new String[]{"选项A", "选项B", "选项C", "选项D"});
            q.setAnswer("选项A");
            questions.add(q);
        }

        // 添加判断题
        for (int i = 6; i <= 8; i++) {
            Question q = new Question();
            q.setId(i);
            q.setType("judge");
            q.setQuestion("判断题" + (i-5) + ": 这是一个示例判断题。");
            q.setAnswer("true");
            questions.add(q);
        }

        // 添加填空题
        for (int i = 9; i <= 10; i++) {
            Question q = new Question();
            q.setId(i);
            q.setType("fill");
            q.setQuestion("填空题" + (i-8) + ": 这是一个示例_____题。");
            q.setAnswer("填空");
            questions.add(q);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(gson.toJson(questions));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        // 读取请求体
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = request.getReader().readLine()) != null) {
            sb.append(line);
        }

        // 将JSON转换为Map
        Map<String, String> answers = gson.fromJson(sb.toString(),
                new TypeToken<Map<String, String>>(){}.getType());

        // 计算得分
        int score = 0;
        for (Question question : questions) {
            String userAnswer = answers.get(String.valueOf(question.getId()));
            if (userAnswer != null && userAnswer.equals(question.getAnswer())) {
                // 根据题目类型分配分数
                switch (question.getType()) {
                    case "single":
                        score += 10; // 单选题10分
                        break;
                    case "judge":
                        score += 10; // 判断题10分
                        break;
                    case "fill":
                        score += 15; // 填空题15分
                        break;
                }
            }
        }

        // 使用 HashMap 替代 Map.of
        Map<String, Integer> result = new HashMap<>();
        result.put("score", score);
        response.getWriter().write(gson.toJson(result));
    }
}